
const routerPath = {
     index:"/pages/main/index", // 首页
     login:"/packageA/login/login", // 登录
     backWord:"/packageA/login/backWord/index",//找回密码
     spare:"/pages/manage/spare/spare", // 备件管理
     equipment:"/pages/equipment/index",//设备
     eqDetail:"/pages/equipment/eqDetail/index", // 设备详情
     zkDetail:"/pages/equipment/zkDetail/index", // 质控详情
     repair:"/pages/repair/index",// 报修菜单
     userhome:"/pages/index/userhome/index",//内部员工首页
     submit:"/pages/repair/submit/index",//一键报修
     backOrder:"/pages/repair/workBack/back", // 撤销工单
     editOrder:"/pages/repair/workEdit/edit", // 编辑工单
     workLog:"/pages/repair/workLog/index",// 工单记录
     workDeal:"/pages/repair/workDeal/index",// 处理工单
     workTrans:'/pages/repair/workTrans/index',// 转单
     workReport:"/pages/repair/report/index", // 生成报告
     control:"/pages/control/index",// 质控
     qualityControl:"/pages/qualityControl/index",// 质控
     executeControl:"/pages/qualityControl/executeControl/index",// 执行质控
     controlRecord:"/pages/qualityControl/controlRecord/index",// 质控记录
     definQuality:"/pages/qualityControl/definQuality/index",// 自定义质控项
     addDefine:"/pages/qualityControl/definQuality/addDefin/index",// 新增自定义质控项
     defineDetail:"/pages/qualityControl/definQuality/detailDefin/index",// 自定义质控项详情
     edtionDefin:"/pages/qualityControl/definQuality/edtionDefin/index",// 新增自定义质控项
     equipmentManage:"/pages/qualityControl/equipmentManage/index",// 质控设备管理
     addEquipment:"/pages/qualityControl/equipmentManage/addEquipment/index",// 质控设备管理
     healthManage:"/packageA/healthManage/index",//健康管理
     addhealthManage:"/packageA/healthManage/addManage/index",//新增计划管理
     dealManage:"/packageA/healthManage/dealManage/index",//执行计划
     person:"/pages/person/index", // 个人中心
     message:"/pages/person/message/index",//消息通知
     messageDel:"/pages/person/messageDetail/index",//消息详情
     passWord:"/pages/person/passWord/index",//修改密码
     waring:"/pages/waring/index"//智能预警
}

class Router{
    constructor(){

    }
    /**
     * 将json对象分割为字符串
     * @param {object} data 
     * @return {string}
     */
    parse(data){
        let tempArr = []
        // 这里'/'会被转义掉 可以尝试手动填值
        for(let key in data){
            tempArr.push(key + '=' + encodeURIComponent(data[key]))
        }
        return tempArr.join('&')
    }
    /**
     * 传入地址和参数后将会携参跳转 vue router的push
     * @param {string} path 
     * 或者
     * @param {object} option 
     * option{
     *  path: 路径
     *  query{} 参数
     *  openType 跳转类型
     *  duration 延时
     * }
     */
    push(path,option = {}){
        if(typeof path == 'string'){
            option.path = path // 监控无参数路径
        } else {
            option = path
        }
        // 匹配key值找到对应的path
        let url = routerPath[option.path] || routerPath["index"]
        // 读取传入的配置c参数
        let {query={}, openType = '', duration = 0} = option
        // json 转换为字符串拼接参数
        let params = this.parse(query);
        url = url + '?' + params
        // 是否需要延时跳转
        if(duration){
            setTimeout(()=>{
            this.to(openType, url)
            },duration)
        }else{
            this.to(openType, url)
        }
    }
    /**
     * 进行小程序的路由跳转
     * @param {string} openType  
     * @param {obj} url 
     * openType:{
     *  
     * }
     */
    to(openType,url){
        let obj = {url}
        if(openType == 'redirectTo'){
            wx.redirectTo(obj) // 关闭当前页面，跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面
        } else if(openType == "reLaunch"){
            wx.reLaunch(obj) // 关闭所有页面 打开到应用内的某个页面
            console.log('怀疑没触发')
        } else if(openType == 'back'){
            // 关闭当前页面 返回到上一页面或多级页面  getCurrentPage获取当前页面栈
            wx.navigateBack({
                delta: 1
            });
            console.log('当前页面栈', getCurrentPages())
        } else {
            wx.navigateTo(obj)
        }
    }
    
}
let obj = new Router()
module.exports = obj